We're going to continue where we left off on types by learning how to create our own custom types.
Creating our own types can be useful for several reasons, but one reason is that creating custom types
also allows for us to create a default type that is needed for some kind of system.
For example, maybe we have a custom crafting or inventory system that requires each crafting system
to be set up in a particular way.
We can achieve and enforce a particular setup using a custom type.
Custom types are also incredibly useful for object oriented programming, where we have a class and
enforce the return of a constructor to produce a variable of a specific custom type, and we can also
create custom types for the parameter section of a constructor.
So someone knows exactly what needs to be fulfilled and what is optional to give to the constructor.
You'll see us use custom types later in the course, but just know that Roblox gives us the ability
to do this, even if it seems a little useless at the moment.
So to create a custom type, we can use the keyword type, and then afterwards we can just pass the
name like we would for a variable.
So I'll call this one example table.
And then we can set it equal to an empty table.
And then we could also pass some parameters in here that this type may have.
So we could do example param.
And this parameter needs to be a number.
So now we have this custom type here that any variable we set to have this type must be a table.
And it has to contain this example parameter in there with a number attached to it.
So if I create a new variable like some table and I set it to be of the type example table, as you
can see now, it appears when we go out and look for types, and then we can set it equal to a new table.
And then we can create the example parameter.
As you can see it shows up right here.
And it tells us it needs to be a number because we have set it to be of the type example table.
So we can go ahead and fulfill this just like that.
Now the table is stored at.
This variable is considered sealed, meaning that you can't add or you cannot remove any parameters
from this table.
As an example, if I index some table and try to add another parameter like, we'll call it another
param.
And we set it equal to a number like three.
As you can see, we're going to get an error here.
And it says cannot add property.
Another parameter to example table.
This is because this table is sealed.
Tables are sealed when we explicitly define a type for a table to be.
Tables are unsealed when we just create them like this.
So if I do another table.
And just do that.
This table is considered unsealed and we can add and remove anything we'd like from the table, because
we didn't explicitly define a type for this table.
So this is unsealed and the one above is sealed.
As another example, I'm going to create a type and I'm just going to call it inventory.
And inside of this table I can define some things that an inventory would have.
For example it may contain a table that has, you know, items inside of it.
So what we could do to be able to add multiple things to this items table is that we could define that
the key has to be a number.
And then we can define the value attached to this number is a string.
And then maybe we could add an optional parameter to this table, like the max capacity of the inventory.
And we'll set it to a number.
And then we put that question mark there to make this an optional parameter.
Now when we go to create a new inventory, we could do new inventory.
Declare it of the type inventory and set it equal to a table with some items in it, and just set it
to, I guess, an empty table.
From this point, we can index new inventory and get the items table, and now we can add new index
value pairs.
For example, at this first index I could create something like I don't know, we have a rock right?
And then maybe at the second index we have an item like, I don't know, wood.
And then at the third index.
We have something like a crystal.
And then once we've created these items in this inventory, we can go ahead and print them out in the
console.
So new inventory, we can get the item at the first index.
And then when we go to run our game, we should get rock printed in the console.
And we do.
Perfect.
And of course, one more thing to take a look at is that when we type out new inventory, we can see
it is of our custom type inventory.
So this was just a brief overview of how we can create our own custom types in Roblox.
While it may seem a little useless at first, there are going to be quite useful when we're going to
start using them in module scripts and our classes.
See you in the next lecture.